package org.example.operation;
import java.util.*;
import org.example.model.*;

/**
 * @author lenovo
 */

public class SortOperation {
    //对HashMap按照num排序，返回排序的ArrayList结果
    public static ArrayList<Score> sortNum(){
        ScoreOperation.loadScore();
        ArrayList<Score> arrayList = new ArrayList<>(Save.getHashMapScore().values());
        arrayList.sort(Comparator.comparing((score)-> score.getStudent().getNum()));
        return arrayList;
    }
    //计算每一个Map.Entry的averageScore，返回计算后的HashMap
    public static HashMap<String,Score> averageScore(){
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        for(Map.Entry<String,Score> map : hashMap.entrySet()){
            double sum = 0.0;
            for(Map.Entry<String,CourseScore> mapScore : map.getValue().getScore().entrySet()){
                sum = sum + mapScore.getValue().getScore();
            }
            if(!map.getValue().getScore().isEmpty()){
                sum = sum / map.getValue().getScore().size();
            }
            map.getValue().setAverage(sum);
            hashMap.put(map.getKey(),map.getValue());
        }
        return hashMap;
    }
    //对HashMap按照averageScore排序，返回排序的ArrayList结果
    public static ArrayList<Score> sortScore(){
        ScoreOperation.loadScore();
        ArrayList<Score> arrayList = new ArrayList<>(averageScore().values());
        arrayList.sort((score1,score2)-> {
            if(score2.getAverage()-score1.getAverage() < -1e-12 ||
                    (score2.getScore().isEmpty() && !score1.getScore().isEmpty())) {
                return -1;
            }else if(score2.getAverage()-score1.getAverage() > 1e-12 ||
                    (!score2.getScore().isEmpty() && score1.getScore().isEmpty())) {
                return 1;
            }else{
                return score1.getStudent().getNum().compareTo(score2.getStudent().getNum());
            }
        });
        return arrayList;
    }
    //计算每一个course与averageScore，返回计算后的HashMap
    public static Score averageCourseScore(){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Course> hashMapCourse = Save.getHashMapCourse();
        HashMap<String,Score> hashMapScore = averageScore();
        HashMap<String,CourseScore> hashMapCourseScore = new HashMap<>();
        Score score = new Score();
        for (Map.Entry<String,Course> mapCourse : hashMapCourse.entrySet()) {
            Course course = new Course();
            course.setCourseName(mapCourse.getKey());
            CourseScore courseScore = new CourseScore();
            courseScore.setCourse(course);
            double sum = 0;
            int count = 0;
            for (Map.Entry<String, Score> mapScore : hashMapScore.entrySet()) {
                if (mapScore.getValue().getScore().containsKey(mapCourse.getKey())) {
                    sum = sum + mapScore.getValue().getScore().get(mapCourse.getKey()).getScore();
                    count++;
                }
            }
            if (count != 0) {
                sum = sum / count;
                courseScore.setScore(sum);
                hashMapCourseScore.put(mapCourse.getKey(), courseScore);
            }
        }
        double sum2 = 0;
        int count2 = 0;
        for(Map.Entry<String,Score> map : hashMapScore.entrySet()){
            if(!map.getValue().getScore().isEmpty()){
                sum2 = sum2 + map.getValue().getAverage();
                count2++;
            }
        }
        if(count2 != 0){
            sum2 = sum2 / count2;
        }
        score.setScore(hashMapCourseScore);
        score.setAverage(sum2);
        return score;
    }
    //计算courseName中每个段的人数，返回对应的ArrayList
    public static ArrayList<Integer> courseSegmentScore(String courseName){
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = averageScore();
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0 ; i < 9 ; i++){
            arrayList.add(0);
        }
        for (Map.Entry<String,Score> map : hashMap.entrySet()){
            boolean notEmpty;
            double score;
            if(courseName.isEmpty()){
                notEmpty = !map.getValue().getScore().isEmpty();
            }else{
                notEmpty = map.getValue().getScore().containsKey(courseName);
            }
            if(notEmpty) {
                if(courseName.isEmpty()){
                    score = map.getValue().getAverage();
                }else{
                    score = map.getValue().getScore().get(courseName).getScore();
                }
                if(score < 60-1e-12) {
                    arrayList.set(0, arrayList.get(0) + 1);
                }else if(score < 65-1e-12) {
                    arrayList.set(1, arrayList.get(1) + 1);
                }else if(score < 70-1e-12) {
                    arrayList.set(2, arrayList.get(2) + 1);
                }else if(score < 75-1e-12) {
                    arrayList.set(3, arrayList.get(3) + 1);
                }else if(score < 80-1e-12) {
                    arrayList.set(4, arrayList.get(4) + 1);
                }else if(score < 85-1e-12) {
                    arrayList.set(5, arrayList.get(5) + 1);
                }else if(score < 90-1e-12) {
                    arrayList.set(6, arrayList.get(6) + 1);
                }else if(score < 95-1e-12) {
                    arrayList.set(7, arrayList.get(7) + 1);
                }else{
                    arrayList.set(8, arrayList.get(8) + 1);
                }
            }
        }
        return arrayList;
    }
}
